package what.the.fun.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import what.the.fun.domain.Answer;
import what.the.fun.domain.AnswerResult;
import what.the.fun.domain.QuestAnsList;
import what.the.fun.domain.Question;
import what.the.fun.domain.Result;
import what.the.fun.domain.Survey;
import what.the.fun.services.AnswerResultService;
import what.the.fun.services.AnswerService;
import what.the.fun.services.QuestionService;
import what.the.fun.services.ResultService;
import what.the.fun.services.SurveyService;

@Controller
public class AnswerSurveyController {
	
	@Autowired
	SurveyService surveyService;
	@Autowired
	QuestionService questionService;
	@Autowired
	AnswerService answerService;
	@Autowired
	ResultService resultService;
	@Autowired
	AnswerResultService answerResultService;
	
	String guestId = "USER000000";
	
	@RequestMapping("/answer-survey")
	public ModelAndView answerSurvey(@RequestParam String surveyId) {
		ModelMap model = new ModelMap();
		List<QuestAnsList> listQuestAnsList = new ArrayList<QuestAnsList>();
		List<Question> listQuestion = new ArrayList<Question>();
		listQuestion = questionService.getQuestionListBySurveyId(surveyId);
		
		QuestAnsList questAnsList;
		for(Question question: listQuestion) {
			questAnsList = new QuestAnsList();
			questAnsList.setQuestion(question);
			questAnsList.setAnswerList(answerService.getAnswerListByQuestId(question.getQuestId()));
			listQuestAnsList.add(questAnsList);
		}
		
		
		Survey survey = surveyService.getData(surveyId);
		String campId = survey.getCampId();
		model.addAttribute("survey", survey);
		model.addAttribute("campId", campId);
		
		model.put("listQuestAnsList", listQuestAnsList);
		
		return new ModelAndView("answer-survey/answer-survey", "model", model);
	}
	
	
	@RequestMapping("/answer-result-save/{surveyId}")
	public String saveAnswerResult(HttpServletRequest request, HttpSession session
											,@PathVariable("surveyId") String surveyId
											) {
		List<Question> questList = questionService.getQuestionListBySurveyId(surveyId);
		List<String> selectedAnswerId = new ArrayList<String>();
		String[] requestAnswer;
		for(Question q: questList) {
			requestAnswer = request.getParameterValues("checkbox" + q.getQuestId());
			if (requestAnswer != null) {
				for(String s: requestAnswer) {
					selectedAnswerId.add(s);
				}
			}
		}
		
		Result result;
		// check user login
		if (session.getAttribute("userId") != null) {
			result = new Result(surveyId, session.getAttribute("userId").toString());
		}
		else {
			result = new Result(surveyId, guestId);
		}
		
		resultService.insertData(result);	// insert new result into database
		String resultId = resultService.getLast().getResultId();
		AnswerResult answerResult;
		
		for(String id: selectedAnswerId) {
			
			answerResult = new AnswerResult();
			answerResult.setResultId(resultId);
			answerResult.setAnswerId(id);
			answerResultService.insertData(answerResult);
		}
		
		String campId = surveyService.getData(surveyId).getCampId();
		return "redirect:/camp-detail?campId=" + campId;
	}
	
	@RequestMapping("/answer-result")
	public ModelAndView answerResult(HttpServletRequest request) {
		String[] selectedAnswerId = request.getParameterValues("checkbox");
		Question question;
		Answer answer;
		List<Answer> answerList;
		List<QuestAnsList> listQuestAnsList = new ArrayList<QuestAnsList>();
		for(String id: selectedAnswerId) {
			answer = answerService.getData(id);
			question = questionService.getData(answer.getQuestId());
			answerList = new ArrayList<Answer>();
			answerList.add(answer);
			listQuestAnsList.add(new QuestAnsList(question, answerList));
		}
		//TODO: save answer list into table answer-list
		
		ModelMap model = new ModelMap();
		model.put("listQuestAnsList", listQuestAnsList);
		
		return new ModelAndView("answer-survey/answer-result", "model", model);
	}
}
